/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hyk.appcfg.wrapper.invoker;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hyk.appcfg.wrapper.prefer.Preference.AppType;
import org.hyk.appcfg.wrapper.util.SimpleTextFileModifier;
import org.hyk.appcfg.wrapper.util.SimpleXmlFileModifier;

/**
 *
 * @author Administrator
 */
public class UpdateTask extends AppCfgTask {

    public boolean hasBackends;
    String appid;

    private UpdateTask() {
        super(null, null, null, null);
    }

    public String getVersion() {
        return version;
    }

    public String getAppid() {
        return appid;
    }
    String version;

    public UpdateTask(AppType appType, String email, String passwd, String appDir, String appid, String version) {
        super(appType, email, passwd, appDir);
        this.appid = appid;
        this.version = version;
    }

    public UpdateTask clone() {
        UpdateTask task = new UpdateTask();
        task.hasBackends = hasBackends;
        task.version = version;
        task.appid = appid;
        task.appType = appType;
        task.email = email;
        task.passwd = passwd;
        task.appDir = appDir;
        return task;
    }

    public String getAction() {
        return hasBackends ?"backends update":"update";
    }

    @Override
    public void preExecute() {
        try {
            switch (appType) {
                case JAVA: {
                    String appenginexml = appDir + FILE_SP + "WEB-INF" + FILE_SP + "appengine-web.xml";
                    SimpleXmlFileModifier.modify(appenginexml, "application", appid);
                    SimpleXmlFileModifier.modify(appenginexml, "version", version);
                    break;
                }
                case PYTHON: {
                    String appyaml = appDir + FILE_SP + "app.yaml";
                    SimpleTextFileModifier.modify(appyaml, "application", appid);
                    SimpleTextFileModifier.modify(appyaml, "version", version);
                    break;
                }
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(UpdateTask.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(UpdateTask.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
